package com.hzhk.happyrent.ui.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hzhk.happyrent.Constants;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.bean.person.ServiceBean;
import com.hzhk.happyrent.bean.person.UserInfoModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.activity.bill.BillActivity;
import com.hzhk.happyrent.ui.activity.order.OrderListActivity;
import com.hzhk.happyrent.ui.activity.personal.AddressActivity;
import com.hzhk.happyrent.ui.activity.personal.CallCenterActivity;
import com.hzhk.happyrent.ui.activity.personal.LoginActivity;
import com.hzhk.happyrent.ui.activity.personal.PersonInfoActivity;
import com.hzhk.happyrent.ui.activity.personal.UserInfoActivity;
import com.hzhk.happyrent.ui.activity.setting.SettingActivity;
import com.hzhk.happyrent.ui.fragment.BaseFragment;
import com.hzhk.happyrent.utils.MyPerference;
import com.hzhk.happyrent.utils.glide.ShowImageUtils;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxStringCallback;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * User: hzhk(1097282555@qq.com)
 * Date: 2017/11/21
 * Time: 18:03
 * progject: HappyRent
 * Describe： ${Describe}
 */
public class MyFragment extends BaseFragment {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.activity_titlebar_right)
    ImageView activityTitlebarRight;
    @BindView(R.id.person_header)
    ImageView personHeader;
    @BindView(R.id.person_to_login_or_register)
    TextView personToLoginOrRegister;
    @BindView(R.id.person_user_name)
    TextView personUserName;
    @BindView(R.id.person_logged)
    LinearLayout personLogged;
    @BindView(R.id.identity_info)
    LinearLayout cardForApply;
    @BindView(R.id.wallet)
    LinearLayout wallet;
    @BindView(R.id.ll_address_manage)
    LinearLayout llAddressManage;
    @BindView(R.id.ll_service)
    LinearLayout llService;
    @BindView(R.id.ll_message)
    LinearLayout llMessage;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    Unbinder unbinder;
    @BindView(R.id.ll_bg)
    LinearLayout llBg;
    @BindView(R.id.show_jy)
    TextView showJy;
    private View mFragmentView;
    private String url;

    @Override
    protected View getContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_person, null);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (null == mFragmentView) {
            mFragmentView = super.onCreateView(inflater, container, savedInstanceState);
            initView();
            showJY();
        }
        unbinder = ButterKnife.bind(this, mFragmentView);
        return mFragmentView;
    }

    private void initData() {

        Map<String, Object> params = new HashMap<>();
        params.put("user_id", MyPerference.getInstance().getUserId());
        HttpManager.getInstance().get(API.GET_USER_INFO, params, new RxStringCallback() {
            @Override
            public void onError(Object tag, Throwable e) {
//                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                CodeModel codeModel = new Gson().fromJson(response, CodeModel.class);
                if (0 == codeModel.getErrCode()) {
                    UserInfoModel userInfoModel = new Gson().fromJson(response, UserInfoModel.class);
                    if (!TextUtils.isEmpty(userInfoModel.getData().getNickname())) {
                        personUserName.setText(userInfoModel.getData().getNickname());
                    } else {
                        personUserName.setText(MyPerference.getInstance().getUserPhone());
                    }
                    if (!TextUtils.isEmpty(userInfoModel.getData().getAvatar())) {
                        url = userInfoModel.getData().getAvatar();
                        ShowImageUtils.showImageViewToCircleHolder(getContext(), userInfoModel.getData().getAvatar(), personHeader, R.drawable.person_header);
//                        ShowImageUtils.showImageViewBlur(getContext(), userInfoModel.getData().getAvatar(), R.drawable.person_top_bg,llBg);
                    }
                    MyPerference.getInstance().setAuth(userInfoModel.getData().getIs_auth());
                }
            }

            @Override
            public void onCompleted(Object tag) {
                super.onCompleted(tag);
            }
        });

    }

    private void initView() {
//        activityTitlebarRight.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(MyPerference.getInstance().getUserId())) {
            personToLoginOrRegister.setVisibility(View.VISIBLE);
            personLogged.setVisibility(View.GONE);
            personHeader.setImageDrawable(getResources().getDrawable(R.drawable.person_header));
        } else {
            personToLoginOrRegister.setVisibility(View.GONE);
            personLogged.setVisibility(View.VISIBLE);
            initData();
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (!TextUtils.isEmpty(MyPerference.getInstance().getUserId())) {
                initData();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != mFragmentView) {
            ((ViewGroup) mFragmentView.getParent()).removeView(mFragmentView);
        }
        unbinder.unbind();
    }


    @OnClick({R.id.activity_titlebar_right
            , R.id.identity_info
            , R.id.wallet
            , R.id.person_to_login_or_register
            , R.id.ll_address_manage
            , R.id.person_header
            , R.id.ll_setting
            , R.id.ll_service
            , R.id.ll_message
            , R.id.ll_refund})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_right:
//                Toast.makeText(getContext(), getString(R.string.function_in_development), Toast.LENGTH_SHORT).show();
//                Intent w=new Intent(getContext(), TextActivity.class);
//                startActivity(w);
//                MyPerference.getInstance().clearUserInfo();
//                doit();
                break;
            case R.id.identity_info:
                if (!TextUtils.isEmpty(MyPerference.getInstance().getUserId())) {
                    startActivity(new Intent(getContext(), PersonInfoActivity.class));
                } else {
                    startActivity(new Intent(getContext(), LoginActivity.class));
                }
                break;
            case R.id.wallet://订单中心
                if (!TextUtils.isEmpty(MyPerference.getInstance().getUserId())) {
                    startActivity(new Intent(getContext(), OrderListActivity.class));
                } else {
                    startActivity(new Intent(getContext(), LoginActivity.class));
                }
                break;
            case R.id.person_to_login_or_register:
                startActivity(new Intent(getContext(), LoginActivity.class));
                break;
            case R.id.ll_address_manage:
                if (!TextUtils.isEmpty(MyPerference.getInstance().getUserId())) {
                    startActivity(new Intent(getContext(), AddressActivity.class));
                } else {
                    startActivity(new Intent(getContext(), LoginActivity.class));
                }
                break;
            case R.id.person_header:
                if (!TextUtils.isEmpty(MyPerference.getInstance().getUserId())) {
                    Intent intent = new Intent(getContext(), UserInfoActivity.class);
                    intent.putExtra(Constants.USER_HEADER, url);
                    intent.putExtra(Constants.USER_NICK_NAME, personUserName.getText().toString());
                    startActivity(intent);
                } else {
                    startActivity(new Intent(getContext(), LoginActivity.class));
                }
                break;
            case R.id.ll_setting:
                startActivity(new Intent(getContext(), SettingActivity.class));
                break;
            case R.id.ll_service:
                startActivity(new Intent(getContext(), CallCenterActivity.class));
                break;
            case R.id.ll_message:
                // TODO: 2018/1/15  跳转预约还机测试使用
                //startActivity(new Intent(getContext(), PreOrderActivity.class));
                // TODO: 2018/1/15  跳转预约还机测试使用

                break;
            case R.id.ll_refund:
                if (!TextUtils.isEmpty(MyPerference.getInstance().getUserId())) {
                    startActivity(new Intent(getContext(), BillActivity.class));
                } else {
                    startActivity(new Intent(getContext(), LoginActivity.class));
                }
                break;
        }
    }

//    private void doit() {
//        new TestZhimaCustomerCertificationQuery(getContext()).testZhimaAuthInfoAuthorize();
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (1 == requestCode) {
            if (data != null && !TextUtils.isEmpty(data.getStringExtra("url"))) {

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    private void showJY() {
        HttpManager.getInstance().get(API.APP_CONFIG, new HashMap<String, Object>(), new MyStringResponse(getContext()) {
            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                ServiceBean serviceBean = new Gson().fromJson(response, ServiceBean.class);
                if (serviceBean != null && serviceBean.getErrCode() == 0) {
                    if (serviceBean.getData() != null && !TextUtils.isEmpty(serviceBean.getData().getIs_show_jy())) {
                        if ("1".equals(serviceBean.getData().getIs_show_jy())) {
                            showJy.setVisibility(View.VISIBLE);
                        }else{
                            showJy.setVisibility(View.GONE);
                        }
                    }
                }
            }
        });
    }
}
